home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / info < prev    next >
Encoding:
Text File  |  2007-04-04  |  6.2 KB  |  231 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22.  
  23. __version__ = '3.4'
  24. __title__ = 'Device Information Utility'
  25. __doc__ = "Query a printer for both static model information and dynamic status."
  26.  
  27. # Std Lib
  28. import sys, getopt, time
  29.  
  30. # Local
  31. from base.g import *
  32. from base import device, status, utils
  33. from prnt import cups
  34.  
  35. USAGE = [(__doc__, "", "name", True),
  36.          ("Usage: hp-info [PRINTER|DEVICE-URI] [OPTIONS]", "", "summary", True),
  37.          utils.USAGE_ARGS,
  38.          utils.USAGE_DEVICE,
  39.          utils.USAGE_PRINTER,
  40.          utils.USAGE_SPACE,
  41.          utils.USAGE_OPTIONS,
  42.          ("Device ID mode:", "-i or --id (prints device ID only and exits)", "option", False),
  43.          utils.USAGE_BUS1, utils.USAGE_BUS2,
  44.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  45.          utils.USAGE_HELP,
  46.          utils.USAGE_SPACE,
  47.          utils.USAGE_NOTES,
  48.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  49.          utils.USAGE_SEEALSO,
  50.          ("hp-toolbox", "", "seealso", False),
  51.  
  52.          ]
  53.  
  54. def usage(typ='text'):
  55.     if typ == 'text':
  56.         utils.log_title(__title__, __version__)
  57.  
  58.     utils.format_text(USAGE, typ, __title__, 'hp-info', __version__)
  59.     sys.exit(0)
  60.  
  61.  
  62. log.set_module('hp-info')
  63.  
  64.  
  65. try:
  66.     opts, args = getopt.getopt(sys.argv[1:], 'p:d:hl:b:ig',
  67.         ['printer=', 'device=', 'help', 'help-rest', 'help-man', 
  68.          'help-desc', 'logging=', 'id', 'bus='])
  69.  
  70. except getopt.GetoptError:
  71.     usage()
  72.  
  73. printer_name = None
  74. device_uri = None
  75. log_level = logger.DEFAULT_LOG_LEVEL
  76. bus = "cups,par,usb"
  77. devid_mode = False
  78.  
  79. if os.getenv("HPLIP_DEBUG"):
  80.     log.set_level('debug')
  81.  
  82. for o, a in opts:
  83.     if o in ('-h', '--help'):
  84.         usage()
  85.  
  86.     elif o == '--help-rest':
  87.         usage('rest')
  88.  
  89.     elif o == '--help-man':
  90.         usage('man')
  91.  
  92.     elif o == '--help-desc':
  93.         print __doc__,
  94.         sys.exit(0)
  95.  
  96.     elif o in ('-p', '--printer'):
  97.         if a.startswith('*'):
  98.             printer_name = cups.getDefault()
  99.             log.info("Using CUPS default printer: %s" % printer_name)
  100.             log.debug(printer_name)
  101.         else:
  102.             printer_name = a
  103.  
  104.     elif o in ('-d', '--device'):
  105.         device_uri = a
  106.  
  107.     elif o in ('-b', '--bus'):
  108.         bus = a.lower().strip()
  109.         if not device.validateBusList(bus):
  110.             usage()
  111.  
  112.     elif o in ('-l', '--logging'):
  113.         log_level = a.lower().strip()
  114.         if not log.set_level(log_level):
  115.             usage()
  116.  
  117.     elif o == '-g':
  118.         log.set_level('debug')
  119.  
  120.     elif o in ('-i', '--id'):
  121.         devid_mode = True
  122.  
  123.  
  124. if device_uri and printer_name:
  125.     log.error("You may not specify both a printer (-p) and a device (-d).")
  126.     usage()
  127.  
  128. if not devid_mode:
  129.     utils.log_title(__title__, __version__)
  130.  
  131. if not device_uri and not printer_name:
  132.     try:
  133.         device_uri = device.getInteractiveDeviceURI(bus)
  134.         if device_uri is None:
  135.             sys.exit(1)
  136.     except Error:
  137.         log.error("Error occured during interactive mode. Exiting.")
  138.         sys.exit(1)
  139.         
  140.  
  141. try:
  142.     d = device.Device(device_uri, printer_name)
  143. except Error:
  144.     log.error("Error opening device. Exiting.")
  145.     sys.exit(1)
  146.  
  147. if d.device_uri is None and printer_name:
  148.     log.error("Printer '%s' not found." % printer_name)
  149.     sys.exit(1)
  150.  
  151. if d.device_uri is None and device_uri:
  152.     log.error("Malformed/invalid device-uri: %s" % device_uri)
  153.     sys.exit(1)
  154.  
  155. if not devid_mode:
  156.     log.info("")
  157.     log.info(utils.bold(d.device_uri))
  158.     log.info("")
  159.  
  160. user_cfg.last_used.device_uri = d.device_uri
  161.  
  162. try:
  163.     d.open()
  164.     d.queryDevice()
  165. except Error, e:
  166.     log.error("Error opening device (%s). Exiting." % e.msg)
  167.     sys.exit(1)
  168.  
  169. if not devid_mode:
  170.     formatter = utils.TextFormatter(
  171.                     (
  172.                         {'width': 28, 'margin' : 2},
  173.                         {'width': 58, 'margin' : 2},
  174.                     )
  175.                 )
  176.  
  177. if devid_mode:
  178.     try:
  179.         print d.dq['deviceid']
  180.     except KeyError:
  181.         log.error("Device ID not available.")
  182. else:
  183.     dq_keys = d.dq.keys()
  184.     dq_keys.sort()
  185.  
  186.     log.info(utils.bold("Device Parameters (dynamic data):"))
  187.     log.info(utils.TextFormatter.bold(formatter.compose(("Parameter", "Value(s)"))))
  188.     log.info(formatter.compose(('-'*28, '-'*58)))
  189.  
  190.     for key in dq_keys:
  191.         log.info(formatter.compose((key, str(d.dq[key]))))
  192.  
  193.     log.info(utils.bold("\nModel Parameters (static data):"))
  194.     log.info(utils.TextFormatter.bold(formatter.compose(("Parameter", "Value(s)"))))
  195.     log.info(formatter.compose(('-'*28, '-'*58)))
  196.  
  197.     mq_keys = d.mq.keys()
  198.     mq_keys.sort()
  199.  
  200.     for key in mq_keys:
  201.         log.info(formatter.compose((key, str(d.mq[key]))))
  202.  
  203.     formatter = utils.TextFormatter(
  204.                     (
  205.                         {'width': 20, 'margin' : 2}, # date/time
  206.                         {'width': 5, 'margin' : 2}, # code
  207.                         {'width': 40, 'margin' : 2}, # desc
  208.                         {'width': 8, 'margin' : 2}, # user
  209.                         {'width': 8, 'margin' : 2}, # job id
  210.                     )
  211.                 )
  212.  
  213.  
  214.     log.info(utils.bold("\nStatus History (most recent first):"))
  215.     log.info(utils.TextFormatter.bold(formatter.compose(("Date/Time", "Code", "Status Description", "User", "Job ID"))))
  216.     log.info(formatter.compose(('-'*20, '-'*5, '-'*40, '-'*8, '-'*8)))
  217.  
  218.     hq = d.queryHistory()
  219.  
  220.     for h in hq:
  221.         if h[9]:
  222.             j = str(h[9])
  223.         else:
  224.             j = ''
  225.         log.info(formatter.compose((time.strftime("%x %H:%M:%S", h[:9]),  str(h[11]), h[12], h[10], j)))
  226.  
  227.     log.info("")
  228.  
  229. d.close()
  230. sys.exit(0)
  231.